package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class FileController {
    @Autowired
    private FileService fileService;
    ///pic/upload?dir=image
    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IOException {
        //获取图片的名字
        String name =fileImage.getOriginalFilename();
        //定义文件目录
        String fileDirPath = "E:/image";
        //创建目录
        File fileDir = new File(fileDirPath);
        //判断目录是否存在
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        //生成文件的全路径
        String filePath =fileDirPath+"/"+name;
        File imageFile=new File(filePath);
        //实现文件上传
        fileImage.transferTo(imageFile);
        return "文件上传成功";
    }

    @RequestMapping("/pic/upload")
    public ImageVO upload(MultipartFile uploadFile){

        return fileService.upload(uploadFile);
    }
}
